import { accountLoginRequest } from "@/service/login/login.js";
import cache from "@/utils/cache.js";
import router from "@/router/index.js";
import { ElMessage } from "element-plus";
import mapMenusToRoutes from "@/utils/map-menus";

const login = {
  namespaced: true,
  state: {
    token: "",
    userInfo: {},
    userMenu: [
      {
        icon: "el-icon-monitor",
        id: 38,
        name: "系统总览",
        sort: 1,
        type: 1,
        url: "/main/analysis",
        children: [
          {
            id: 39,
            children: null,
            name: "账号修改",
            parentId: 38,
            sort: 106,
            type: 2,
            url: "/main/analysis/overview",
          },
          {
            id: 40,
            children: null,
            name: "商品统计",
            parentId: 38,
            sort: 107,
            type: 2,
            url: "/main/analysis/dashboard",
          },
        ],
      },
      {
        id: 1,
        name: "系统管理",
        sort: 2,
        type: 1,
        url: "/main/system",
        iconf: "el-icon-setting",
        children: [
          {
            id: 2,
            children: null,
            name: "用户管理",
            parentId: 1,
            sort: 100,
            type: 2,
            url: "/main/system/user",
          },
          {
            id: 3,
            children: null,
            name: "图书管理",
            parentId: 1,
            sort: 101,
            type: 2,
            url: "/main/system/bookSystem",
          },
        ],
      },
      {
        id: 9,
        children: [
          {
            id: 16,
            children: null,
            name: "个人信息",
            parentId: 9,
            sort: 105,
            type: 2,
            url: "/main/product/goods",
          },
        ],
        name: "个人中心",
        parentId: 1,
        sort: 3,
        icon: "el-icon-goods",
        type: 1,
        url: "/main/product",
      },
    ],
  },
  mutations: {
    changeToken(state, token) {
      state.token = token;
      const userMenu = state.userMenu;
      cache.setCache("userMenu", userMenu);
      const routes = mapMenusToRoutes(userMenu);
      routes.forEach((route) => {
        router.addRoute("main", route);
      });
    },
    changeUserInfo(state, payload) {
      state.userInfo = payload;
    },
  },
  getters: {},
  actions: {
    async accountLoginAction({ commit }, payload) {
      try {
        const result = await accountLoginRequest(payload);
        console.log(result);
        if (result.status != 200) {
          ElMessage.error(result.data);
          return;
        }
        const { id, token } = result.data;
        commit("changeToken", token);
        commit("changeUserInfo", result.data);
        cache.setCache("token", token);
        setTimeout(() => {
          ElMessage({
            message: "登录成功",
            type: "success",
          });
          router.push({
            path: "/main",
          });
        }, 1500);
        return result;
      } catch (error) {
        ElMessage.error(error.data);
        console.log(error);
      }
    },
  },
};

export default login;
